/**
 * Created by dbwyxl on 2017/9/11.
 */
module.exports =function($cookies,$scope, $rootScope, $state,$stateParams,orderReceivingService,Upload,httpservice){

  //获取页面传递的参数
  $scope.params = $state.params;
  $scope.orderReceivingDSkip=orderReceivingDSkip;
  //案件详情案件id
  $scope.caseId = $stateParams.id;
  $scope.init = function () {
    $scope.findConsultOrderInfo();
  };

  //附件信息
  $scope.addConsultOrderMsgList = {};
  //控制附件是否显示
  $scope.isShowUploadFile = true;
  //选择附件的名字
  $scope.selectFileNmae = '';


  //查询案件详情
  $scope.findConsultOrderInfo = function () {
    if($scope.caseId == '' ) {
      return false;
    }
    var newCaseSources={
      'consultOrderId':$scope.caseId//订单id
    };
    orderReceivingService.findConsultOrderInfo(newCaseSources).then(function(data){
      if(data.data.code==1){
        $scope.consultOrderInfoList=data.data.data;
      }
    })
  };

  //回复消息
  $scope.addConsultOrderMsg = function (serviceid,file) {

    var fileSize = file != null ? Math.round(file.size/1024*100)/100 : 0;
    if(file != null && fileSize > (1024*6)){
      //$rootScope.messageAlert('上传文件大于6MB！');
      $scope.errorFileShow = true;
      $scope.errorFileMsg = '上传文件大于6MB！';
      return;
    }
    if(file != null ) {
      $scope.addConsultOrderMsgList.file = file;
    }

    $scope.addConsultOrderMsgList.message = $scope.message;

    $scope.addConsultOrderMsgList.serviceId = serviceid;



    if (true) {
      Upload.upload({
        url: httpservice.addConsultOrderMsgUrl,
        data: $scope.addConsultOrderMsgList
      }).then(function (response) {
        if(response.data.code == 1){
          /*$scope.attesInfo = {};
          $scope.attestStep = 4;
          $scope.authState = 1; */  //认证信息提交成功后，修改认证状态。

          /*$scope.addConsultOrderMsgList=data.data.data;*/
          $scope.findConsultOrderInfo();
          $scope.message = "";

          $scope.addConsultOrderMsgList.message = '';
          $scope.addConsultOrderMsgList.file = null;

          $scope.removeFile();

          return;
        }

      }, function (response) {
        if (response.status > 0)
          $scope.errorMsg = response.status + ': ' + response.data;
      }, function (evt) {

      });
    }

  };
  $scope.getConsultOrderId = function () {
    $("#submit").modal("show");
  };

  //提交验收
  $scope.sumbitAccept = function () {
    var newCaseSources={
      'consultOrderId':$scope.caseId//订单id
    };

    orderReceivingService.sumbitAccept(newCaseSources).then(function(data){
      if(data.data.code==1){
        $scope.sumbitAcceptList=data.data.data;
        $(".modal-backdrop.in").hide();
        $("#submit").modal("hide");
        $state.go("orderReceiving",{},{reload:true});//跳转并刷新 页面
      }
    })
  };

  $scope.init();

  //清空附件
  $scope.removeFile = function () {
    $scope.addConsultOrderMsgList.picFile = null;
    $scope.isShowUploadFile = true;
    $scope.selectFileNmae = '';

  }

  //选择附件
  $scope.selectFile = function (file) {
    $scope.isShowUploadFile = false;
    $scope.selectFileNmae = file.name;
  }

  //下载附件
  $scope.downloadFileProgress = function (file) {
    /*taskId
     taskAttachedId*/
    var params = {'consultOrderMsgId':file.id, 'fileName':file.attrfilename};
    orderReceivingService.fileDownload(params).then(function (response) {
      var fileData = new Blob([response.data]);
      var link = document.createElement('a');
      link.href = URL.createObjectURL(fileData);
      link.download = file.attrfilename;
      link.click();
      // window.URL.revokeObjectURL(link.href);
    });
  };

  //案源下载附件
  $scope.downloadSFileProgress = function (id, name) {
    /*taskId
     taskAttachedId*/
    var params = {'caseSourceId':id, 'fileName':name};
    orderReceivingService.sFileDownload(params).then(function (response) {
      var fileData = new Blob([response.data]);
      var link = document.createElement('a');
      link.href = URL.createObjectURL(fileData);
      link.download = name;
      link.click();
      // window.URL.revokeObjectURL(link.href);
    });
  };
  function orderReceivingDSkip(){
    var parentUrl=sessionStorage.getItem('parentUrl')||'orderReceiving';
    $state.go(parentUrl,{},{reload:true});
    sessionStorage.removeItem('parentUrl')
  }


};
